<?php
session_start();
require_once("lib/util.php");
require_once("lib/db.php");
function get_user_by_email ($email) {
	$login_query ="SELECT * FROM users WHERE email = ?";
	$login_user = query($login_query, array($email));
	if ($login_user == array()) {
		return NULL;}
	else { return $login_user[0];	
		}
	}
	
if ($_POST) {
	connect();
	$user = get_user_by_email($_POST["user_email"]);
	close();
	if(is_null($user)) {
		render("log_in", array("failed" => true));
	} else {
		if ($_POST["password"] == $user["password"]) {
			$_SESSION['logged_in_user'] = $user['user_id'];
			redirect ("/nitpicker/index.php");
		} else {
			render("log_in", array("failed" => true));
		}
	}
} else {
	render("log_in", array("failed" => false));
}

